<template>
	<view>
		<!-- 轮播图 -->
		<u-swiper :list="house.houseImg" :effect3d="true" height="399"></u-swiper>
		<!-- 房型名 房型户型 房型大小 房型价格 房型介绍 房型操作：删除 -->
		<view class="houseInfo">
			<u-field v-model="house.houseName" label="房型名称" placeholder="房型名称" />
			<u-field v-model="house.houseArea" label="房型面积m²" placeholder="请填写房型面积" />
			<u-field v-model="house.housePrice" label="房型价格" placeholder="请填写房型价格" />
			<u-field v-model="house.housePosition" label="区位" placeholder="请填写房型区位" />
			<u-field v-model="house.houseDetailPosition" label="详细地址" placeholder="请填写详细地址" />
			<u-field v-model="house.houseType" label="户型" placeholder="请填写户型" />
			<u-field v-model="house.houseInfo" label="房型介绍" placeholder="请填写房型介绍" />
			<u-field disabled v-model="housePubTime" label="发布时间" placeholder="发布时间" />
		</view>
		<view class="operate">
			<view class="bt1">
				<u-button type="success" @click="update">修改房型信息</u-button>
			</view>
			<view class="bt2">
				<u-button type="error" @click="deleteinfo">删除房型</u-button>
			</view>
			<u-toast ref="uToast" />

		</view>
	</view>
</template>

<script>
	export default {
		onLoad(options) {
			this.house = JSON.parse(options.house)
			this.housePubTime = this.getYMDHMS(this.house.housePubTime)
		},
		data() {
			return {
				house: {},
				housePubTime: ""
			}
		},
		methods: {
			showToast(path) {
				this.$refs.uToast.show({
					title: '操作成功',
					type: 'success',
					url: path 
				})
			},
			update() {
				// 修改房型信息 一是要处理照片
				var that = this
				// 浅复制
				let newhouse = Object.assign({},this.house)
				newhouse.houseImg = newhouse.houseImg.join(";,").toString()
				uni.request({
					url:"http://localhost:8081/api/house/updateHouse",
					method:"PUT",
					data: JSON.stringify(newhouse),
					success:(res)=>{
						that.showToast("/pages/Mypub/Mypub")
					}
				})
			},
			deleteinfo() {
				var that = this
				var houseId = this.house.houseId
				uni.request({
					url:"http://www.zphsow.ltd/api/house/delHouseByPK?houseId="+houseId,
					method:'DELETE',
					success: (res) => {
						if(res.data.result){
							that.showToast("/pages/Mypub/Mypub")
						}
					}
				})
			},
			getYMDHMS(timestamp) {
				let time = new Date(timestamp);
				let year = time.getFullYear();
				let month = time.getMonth() + 1;
				let date = time.getDate();
				if (month < 10) {
					month = "0" + month;
				}
				if (date < 10) {
					date = "0" + date;
				}
				return (
					year +
					"-" +
					month +
					"-" +
					date

				);
			},
		}
	}
</script>

<style>
	.operate {
		width: 60%;
		margin-left: 150rpx;
	}

	.bt1 {
		margin-top: 50rpx;
	}

	.bt2 {
		margin-top: 50rpx;
	}
</style>
